package com.gin.no_gay_alliance.adpter.forum;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.gin.jackson.utils.ObjectUtils;
import com.gin.no_gay_alliance.R;
import com.gin.no_gay_alliance.viewmodel.EntrySubGroup;

import java.util.ArrayList;
import java.util.List;


/**
 * 版面子组
 *
 * @author : ginstone
 * @version : v1.0.0
 * @since : 2023/8/2 17:31
 **/
public class ForumSubGroupAdapter extends RecyclerView.Adapter<ForumSubGroupAdapter.ViewHolder> {
    private final static String TAG = ForumSubGroupAdapter.class.getSimpleName();
    private final Context context;
    private List<EntrySubGroup> data;

    public ForumSubGroupAdapter(List<EntrySubGroup> data, Context context) {
        this.data = data;
        this.context = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //  加载布局文件  填写需要渲染的布局文件
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_forum_sub_group, parent, false);
        return new ViewHolder(view, context);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        // 填充数据到各个子view
        EntrySubGroup subGroup = data.get(position);
        if (ObjectUtils.isEmpty(subGroup.name)) {
            holder.tv.setVisibility(View.GONE);
        } else {
            holder.tv.setVisibility(View.VISIBLE);
            holder.tv.setText(subGroup.name);
        }
        holder.adapter.updateData(subGroup.entries);
    }

    @Override
    public int getItemCount() {
        return data == null ? 0 : data.size();
    }

    public void setData(List<EntrySubGroup> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tv;
        RecyclerView rv;
        ForumEntryAdapter adapter;

        public ViewHolder(@NonNull View itemView, Context context) {
            super(itemView);
            // 使用 findViewById绑定子view

            tv = itemView.findViewById(R.id.tv_sub_group_name);
            rv = itemView.findViewById(R.id.recycler_view);
            int spanCount = 3;
            rv.setLayoutManager(new GridLayoutManager(context, spanCount, LinearLayoutManager.VERTICAL, false));
//            rv.addItemDecoration(new GridItemDecoration(spanCount));
            adapter = new ForumEntryAdapter(context, new ArrayList<>());
            rv.setAdapter(adapter);
        }
    }
}